package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.domain.Material;
import jsu.xxy.coffee3.domain.MaterialNoId;
import jsu.xxy.coffee3.domain.ProductMaterial;
import jsu.xxy.coffee3.service.MaterialService;
import jsu.xxy.coffee3.mapper.MaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张从轩
* @description 针对表【material】的数据库操作Service实现
* @createDate 2023-06-03 06:27:47
*/
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material>
implements MaterialService{
    @Autowired
    MaterialMapper materialMapper;

    @Override
    public Page<Material> getAll(Integer pageNo, Integer pageSize) {
        System.out.println(pageNo + " " + pageSize);
        Page<Material> page = new Page<>(pageNo, pageSize);
        materialMapper.selectPage(page, null);
        return page;
    }

    @Override
    public Page<Material> getMaterial(String name, Integer pageNo, Integer pageSize) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("mName", name);
        Page<Material> page = new Page<>(pageNo, pageSize);
        materialMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Boolean upDataNum(Material material) {

        return materialMapper.updateById(material) > 0;
    }

    @Override
    public Boolean deleteMaterial(Integer id) {

        return materialMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean insertMaterial(MaterialNoId materialNoId) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mname",materialNoId.getMname());

        List<Material> materialList = materialMapper.selectList(queryWrapper);
        if(materialList.size() == 0){
            materialMapper.insertMaterial(materialNoId);
//            return true;
        }
        else {
            materialMapper.changeNums(materialList.get(0).getMid() , materialNoId.getMnum());
        }
        return true;
    }

    @Override
    public Boolean deleteStuffs(List<Integer> ids) {

        return materialMapper.deleteBatchIds(ids) > 0;
    }


}
